//
//  EMSegmentedControl.m
//  ElectronicMall
//
//  Created by 史伟文 on 14/12/27.
//  Copyright (c) 2014年 cczu. All rights reserved.
//
/* 常用字体宏 */
#define HEITIMEDIUM @"STHeitiSC-Medium"     // 黑体－简－中等
#define HEITILIGHT  @"STHeitiSC-Light"      // 黑体－简－细体

/* RGB颜色宏 */
#define RGBA(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]      // RGB颜色值（带透明度）
#define RGB(r,g,b) RGBA(r,g,b,1.0f)     // RGB颜色值（不透明）
#import "EMSegmentedControl.h"

@implementation EMSegmentedControl {
    UIView *segmentBackView;
    NSMutableArray *selectIndexArray;
    CGRect rect;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        selectIndexArray = [NSMutableArray array];
        _titleArray = [NSArray array];
        rect = frame;
    }
    
    return self;
}
- (void)setTitleArray:(NSArray *)titleArray
{
    _titleArray = titleArray;
    [self initializeSegmentView];
}
- (void)initializeSegmentView
{
    segmentBackView = [[UIView alloc] initWithFrame:self.bounds];
    
    [self createSegmentItemWithNum:(int)_titleArray.count];
    
    [self addSubview:segmentBackView];
    
}

- (void)createSegmentItemWithNum: (int)num
{
    CGRect itemRect;
    itemRect.origin.x = 0;
    itemRect.origin.y = 0;
    itemRect.size.width = rect.size.width / num;
    itemRect.size.height = rect.size.height;
    
    for (int i = 0; i < num; i ++) {
        
        UIButton *segItem = [UIButton buttonWithType:UIButtonTypeCustom];
        segItem.frame = itemRect;
        
        UILabel *label = [[UILabel alloc] initWithFrame:segItem.bounds];
        label.text = _titleArray[i];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor darkGrayColor];
        label.font = [UIFont fontWithName:HEITILIGHT size:16];
        [segItem addSubview:label];
        
        CGFloat stringWidth = [_titleArray[i] sizeWithFont:label.font].width;
        CGFloat x = label.bounds.size.width / 2 - stringWidth / 2;
        
        UIView *chooseView = [[UIView alloc] initWithFrame:CGRectMake(x, segItem.frame.size.height - 3, stringWidth, 3)];
        chooseView.backgroundColor = i == 0 ? RGB(113, 178, 81) : [UIColor clearColor];
        
        segItem.tag = 40100 + i;
        [selectIndexArray addObject:segItem];
        [segItem addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventTouchUpInside];
        [segItem addSubview:chooseView];
        
        
        [segmentBackView addSubview:segItem];
        itemRect.origin.x += itemRect.size.width;
        
    }
    
    
    
}

- (void)segmentValueChanged: (UIButton *)sender
{
    self.selectedIndex = sender.tag - 40100;
    
    for (int i = 0; i < selectIndexArray.count; i ++) {
        UIView *select = selectIndexArray[i];
        UIView *selectedView = sender.subviews[1];
        
//        UIButton *bb = segmentBackView.subviews[0];
//        UILabel *label = bb.subviews[0];
//        label.textColor = [UIColor greenColor];
//        
//        NSLog(@"%@", bb.subviews);
        
        
        if (sender.tag == select.tag) {
            
            selectedView.backgroundColor = RGB(113, 178, 81);
            NSMutableArray *lastArray = selectIndexArray;
            for (int j = 0; j < lastArray.count; j ++) {
                if (j != i) {
                    UIView *lastSelectView = [lastArray objectAtIndex:j];
                    UIView *lastSelect = lastSelectView.subviews[1];
                    lastSelect.backgroundColor = [UIColor clearColor];
//                    label.textColor = [UIColor grayColor];
                }
                
            }
            
        }
        
    }
    if (self.actionBlock)
        self.actionBlock(self.selectedIndex);
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
